package resourceloader

import (
	"sync"
	"goTestToMod/rate_limiter_test/utility/resource"
)

var illegalLanguage map[LanguageType]bool = map[LanguageType]bool{LanguageTypeZhCn: true, LanguageTypeEnUs: true}

type ResourceType string
type LanguageType string

const (
	ResourceTypeError   ResourceType = "error"
	LanguageTypeZhCn    LanguageType = "zh_CN"
	LanguageTypeEnUs    LanguageType = "en_US"
	DefaultLanguageType LanguageType = LanguageTypeZhCn
)

type resourceManager struct {
	//第一个key资源类型，第二个key语言类型，第三个key,ErrId, value：配置文件value值
	resorces map[ResourceType]map[LanguageType]map[string]string
}

var instance *resourceManager
var once sync.Once

func GetInstance() *resourceManager {
	//no has Resettime()
	once.Do(func() {
		instance = new(resourceManager)
		instance.resorces = initResource()
	})
	return instance
}

func (this resourceManager) GetErrorResource(langType LanguageType, ErrorId string) (string, bool) {
	message, ok := this.resorces[ResourceTypeError][langType][ErrorId]
	return message, ok
}

func GetLanguageType(language string) LanguageType {
	langType := LanguageType(language)
	if illegalLanguage[langType] {
		return langType
	}
	return DefaultLanguageType
}

func initResource() map[ResourceType]map[LanguageType]map[string]string {
	resources := make(map[ResourceType]map[LanguageType]map[string]string, 0)
	resources[ResourceTypeError] = make(map[LanguageType]map[string]string, 0)
	resources[ResourceTypeError][LanguageTypeZhCn] = resource.ErrorZhCn
	resources[ResourceTypeError][LanguageTypeEnUs] = resource.ErrorEnUs
	return resources
}
